package com.hardydou.concurrent;

import java.util.concurrent.Semaphore;

/**
 * 信号量 使用示例
 *
 * @author hardy
 */
public class SemaphoreTrain implements Runnable {
    //车牌号
    private int num;

    //信号灯
    private Semaphore semaphore;

    public SemaphoreTrain(int num, Semaphore semaphore) {
        this.num = num;
        this.semaphore = semaphore;
    }

    public void run() {
        try {
            semaphore.acquire();
            System.out.println("Train No." + num + " pulling in 。");
            Thread.sleep(5L);
            semaphore.release();
            System.out.println("Train No." + num + " pulling out 。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


